package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.dongdong.mapper.TbSpecificationMapper;
import com.dongdong.mapper.TbSpecificationOptionMapper;
import com.dongdong.model.TbSpecification;
import com.dongdong.model.TbSpecificationExample;
import com.dongdong.model.TbSpecificationOption;
import com.dongdong.model.TbSpecificationOptionExample;
import com.dongdong.service.SpecService;
import com.dongdong.utils.PageBean;
import com.dongdong.utils.PageResult;
import com.dongdong.vo.SpecOptionVO;
import com.dongdong.vo.SpecVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private TbSpecificationMapper specMapper;
    @Autowired
    private TbSpecificationOptionMapper specOptionMapper;

    @Override
    public PageResult<SpecVO> getSpecByPageLike(PageBean<SpecVO> pageBean) {
        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        if (!StrUtil.isEmpty(pageBean.getParams().getSpecName())){
            criteria.andSpecNameLike("%"+pageBean.getParams().getSpecName()+"%");
        }
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<TbSpecification> specLists = specMapper.selectByExample(example);
        List<SpecVO> voList = specLists.stream().map((x) -> {
            SpecVO vo = new SpecVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        PageInfo<TbSpecification> info = new PageInfo<>(specLists);
        PageResult<SpecVO> pageResult = new PageResult<>(info.getPageNum(),info.getPageSize(),info.getTotal(),voList);
        return pageResult;
    }

    @Override
    @Transactional
    public void addSpec(SpecVO specVO) {
        TbSpecification spec = new TbSpecification();
        BeanUtils.copyProperties(specVO,spec);
        specMapper.insertSpec(spec);


        List<SpecOptionVO> optionVOS = specVO.getOptionVOS();
        for (SpecOptionVO optionVO : optionVOS) {
            optionVO.setSpecId(spec.getId());
            TbSpecificationOption option = new TbSpecificationOption();
            BeanUtils.copyProperties(optionVO,option);
            specOptionMapper.insertSelective(option);
        }

    }

    @Override
    public SpecVO getSpecAndOptionById(Long id) {

        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);

        List<TbSpecificationOption> options = specOptionMapper.selectByExample(example);
        List<SpecOptionVO> specOptionVOS = options.stream().map((x) -> {
            SpecOptionVO specOptionVO = new SpecOptionVO();
            BeanUtils.copyProperties(x, specOptionVO);
            return specOptionVO;
        }).collect(Collectors.toList());

        TbSpecification specification = specMapper.selectByPrimaryKey(id);
        SpecVO specVO = new SpecVO();
        BeanUtils.copyProperties(specification,specVO);
        specVO.setOptionVOS(specOptionVOS);
        return specVO;
    }

    @Override
    @Transactional
    public void updateSpecAndOption(SpecVO specVO) {
//        更新
        TbSpecification specification = new TbSpecification();
        BeanUtils.copyProperties(specVO,specification);
        specMapper.updateByPrimaryKeySelective(specification);
//        删除
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(specification.getId());
        specOptionMapper.deleteByExample(example);
//        添加
        List<SpecOptionVO> optionVOS = specVO.getOptionVOS();

        List<TbSpecificationOption> options = optionVOS.stream().map((x) -> {
            TbSpecificationOption option = new TbSpecificationOption();
            BeanUtils.copyProperties(x, option);
            return option;
        }).collect(Collectors.toList());

        for (TbSpecificationOption option : options) {
            specOptionMapper.insertSelective(option);
        }

    }

    @Override
    @Transactional
    public void deleteSpecAndOption(Long id) {
//        先删option
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        specOptionMapper.deleteByExample(example);
//        删除spec
        specMapper.deleteByPrimaryKey(id);

    }

    @Override
    public List<SpecVO> getSpecList() {

        List<TbSpecification> list = specMapper.selectByExample(null);
        List<SpecVO> vos = list.stream().map((x) -> {
            SpecVO vo = new SpecVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());

        return vos;
    }
}
